﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Acms.WebControls
{
    public class RadGridDropDownColumn : BaseRadGridColumn
    {
        public string DataValueField { get; set; }
        public string DataSourceID { get; set; }
        public string DropDownValueField { get; set; }
        public string DropDownDisplayField { get; set; }
        public string EmptyText { get; set; }
        public string EmptyValue { get; set; }
        public bool ShowEmptyItem { get; set; }

        protected override string GetControlIdToValidate(Telerik.Web.UI.GridItem item)
        {
            return string.Format("ddl_{0}_{1}", this.UniqueName, item.ItemIndex);
        }

        protected override void InitalizeEditCell(System.Web.UI.WebControls.TableCell cell, int columnIndex, Telerik.Web.UI.GridItem item)
        {
            DropDownList ctrl = new DropDownList();
            ctrl.ID = GetControlIdToValidate(item);
            ctrl.DataSourceID = DataSourceID;
            ctrl.DataTextField = DropDownDisplayField;
            ctrl.DataValueField = DropDownValueField;
            ctrl.DataBinding += new EventHandler(ctrl_DataBinding);
            ctrl.DataBound += new EventHandler(ctrl_DataBound);
            cell.Controls.Add(ctrl);
        }

        void ctrl_DataBinding(object sender, EventArgs e)
        {
            DropDownList ctrl = sender as DropDownList;
            if (ShowEmptyItem || !string.IsNullOrEmpty(EmptyText) || !string.IsNullOrEmpty(EmptyValue))
            {
                ctrl.AppendDataBoundItems = true;
                ListItem tmp = new ListItem();
                if (!string.IsNullOrEmpty(EmptyText))
                {
                    tmp.Text = EmptyText;
                }
                if (!string.IsNullOrEmpty(EmptyValue))
                {
                    tmp.Value = EmptyValue;
                }
                ctrl.Items.Add(tmp);

            }
        }


        void ctrl_DataBound(object sender, EventArgs e)
        {
            DropDownList ctrl = sender as DropDownList;
            object item = GetDataItem(ctrl.NamingContainer);
            if (item != null)
            {
                object val = DataBinder.Eval(item, DataValueField);

                if (val != null)
                {
                    foreach (ListItem i in ctrl.Items)
                    {
                        if (i.Value == val.ToString())
                        {
                            i.Selected = true;
                        }
                    }
                }
            }
        }

        public override void FillValues(System.Collections.IDictionary newValues, Telerik.Web.UI.GridEditableItem editableItem)
        {
            DropDownList ctrl = editableItem.FindControl(GetControlIdToValidate(editableItem)) as DropDownList;
            if (ctrl != null)
            {
                newValues.Add(DataValueField, ctrl.SelectedValue);
                if (DataValueField != DataField && ctrl.SelectedItem != null)
                {
                    newValues.Add(DataField, ctrl.SelectedItem.Text);
                }
            }
        }
    }
}
